package com.example.ecs.systems;

import com.example.ecs.core.Entity;
import com.example.ecs.components.PositionComponent;
import com.example.ecs.components.render.RenderableComponent;
import com.example.ecs.core.System;

import java.awt.Graphics2D;
import java.awt.RenderingHints;

/**
 * RenderSystem类负责渲染所有具有渲染组件的实体
 */
public class RenderSystem extends System {
    private Graphics2D g;
    
    public RenderSystem(Graphics2D g) {
        super();
        this.g = g;
    }
    
    @Override
    public void update(double deltaTime) {
        // 设置抗锯齿渲染提示
        if (g != null) {
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        }
        
        for (Entity entity : entities) {
            if (entity.hasComponent(PositionComponent.class) && entity.hasComponent(RenderableComponent.class)) {
                PositionComponent position = entity.getComponent(PositionComponent.class);
                RenderableComponent renderable = entity.getComponent(RenderableComponent.class);
                
                // 调用渲染组件的渲染方法
                renderable.render(g, position.x, position.y);
            }
        }
    }
    
    public void setGraphics(Graphics2D g) {
        this.g = g;
    }
}